package se.combitech.combiorder.controller;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.inject.Produces;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import se.combitech.combiorder.model.Customer;
import se.combitech.combiorder.service.CustomerService;

// The @Model stereotype is a convenience mechanism to make this a request-scoped bean that has an
// EL name
// Read more about the @Model stereotype in this FAQ:
// http://seamframework.org/Documentation/WhatIsThePurposeOfTheModelAnnotation
@ConversationScoped
@Named
public class CustomerController implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Inject
    private FacesContext facesContext;

    @Inject
    private CustomerService customerService;

    private Customer newCustomer;
    private Customer customer;

    @Produces
    @Named
    public Customer getNewCustomer() {
        return newCustomer;
    }

    @Produces
    @Named
    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public void save() throws Exception {
        try {
            customerService.save(newCustomer);
            FacesMessage m =
                new FacesMessage(FacesMessage.SEVERITY_INFO, "Sparad!", "Användaren sparad");
            facesContext.addMessage(null, m);
            initNewCustomer();
        } catch (Exception e) {
            String errorMessage = getRootErrorMessage(e);
            FacesMessage m =
                new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage,
                        "Användaren kunde ej sparas");
            facesContext.addMessage(null, m);
        }
    }

    public void change() throws Exception {
        try {
            customerService.change(customer);
        } catch (Exception e) {
            String errorMessage = getRootErrorMessage(e);
            FacesMessage m =
                new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage,
                        "Användaren kunde ej sparas");
            facesContext.addMessage(null, m);
        }
    }

    public void delete() throws Exception {
        try {
            customerService.delete(customer);
        } catch (Exception e) {
            String errorMessage = getRootErrorMessage(e);
            FacesMessage m =
                new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage,
                        "Användaren kunde ej tas bort");
            facesContext.addMessage(null, m);
        }
    }

    @PostConstruct
    public void initNewCustomer() {
        newCustomer = new Customer();
    }

    private String getRootErrorMessage(Exception e) {
        // Default to general error message that registration failed.
        String errorMessage = "Registration failed. See server log for more information";
        if (e == null) {
            // This shouldn't happen, but return the default messages
            return errorMessage;
        }

        // Start with the exception and recurse to find the root cause
        Throwable t = e;
        while (t != null) {
            // Get the message from the Throwable class instance
            errorMessage = t.getLocalizedMessage();
            t = t.getCause();
        }
        // This is the root cause message
        return errorMessage;
    }

}
